//
//  CVCellOfSecondBelowReadArticle.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/7.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "CVCellOfSecondBelowReadArticle.h"
#import "BaseTableView.h"
#import "MJMacro.h"
#import "TVCellOfReadAriticle.h"
#import "CycleBannerOfReadAcrticle.h"
#import "ReadAritcleTVCellRootClass.h"
#import "ReadAritcleTVCellData.h"
#import "ReadAritcleNeedTVCellData.h"
#import "ZWYNetTool.h"
#import "UIImageView+WebCache.h"
#import "VCOfReadArticle.h"
#import "MJRefresh.h"
#import "VCOfReadWeb.h"
@interface CVCellOfSecondBelowReadArticle()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) ReadAritcleTVCellRootClass *readAritcleTVCellRootClass;
@property (nonatomic, retain) BaseTableView *readArticleOfTableView;
@property (nonatomic, strong) NSMutableArray<ReadAritcleNeedTVCellData *> *mArrayOfData;
@end
@implementation CVCellOfSecondBelowReadArticle
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
    }
    return self;
}
- (void)setStringURL:(NSString *)stringURL{
    _stringURL = stringURL;
    [self handleData];
}
#pragma mark - 数据解析
- (void)handleData{
    // 下面 TableViewCell 数据的处理
    [ZWYNetTool GET:_stringURL andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _readAritcleTVCellRootClass = [ReadAritcleTVCellRootClass modelDic:result];
        self.mArrayOfData = _readAritcleTVCellRootClass.data.data.mutableCopy;
        [self.readArticleOfTableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
#pragma mark - 创建 TableView
- (void)createTableView{
    self.readArticleOfTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.contentView addSubview:self.readArticleOfTableView];
    self.readArticleOfTableView.delegate = self;
    self.readArticleOfTableView.dataSource = self;
    [self.readArticleOfTableView registerNib:[UINib nibWithNibName:@"TVCellOfReadAriticle" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfReadAriticle"];
    // 下拉刷新
    self.readArticleOfTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 上啦加载
    self.readArticleOfTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
};
#pragma mark - 下拉刷新
- (void)loadNewData{
    [_mArrayOfData removeAllObjects];
    [self handleData];
    [self.readArticleOfTableView reloadData];
    [self.readArticleOfTableView.mj_header endRefreshing];
}
#pragma mark - 上拉加载
- (void)loadMoreData{
    static int i = 2;
    NSString *string = [_stringURL stringByReplacingOccurrencesOfString:@"page=1" withString:@"page=%d"];
    NSString *stringURL = [NSString stringWithFormat:string, i];
    [ZWYNetTool GET:stringURL andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _readAritcleTVCellRootClass = [ReadAritcleTVCellRootClass modelDic:result];
        for (ReadAritcleNeedTVCellData *readAritcleNeedTVCellData in _readAritcleTVCellRootClass.data.data) {
            [_mArrayOfData addObject:readAritcleNeedTVCellData];
        }
        [self.readArticleOfTableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    [self.readArticleOfTableView.mj_footer endRefreshing];
    i++;
}
#pragma mark - tableView 代理方法 row 行数的确定
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _mArrayOfData.count;
}
#pragma mark - tableView 代理方法 cell 的赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TVCellOfReadAriticle *cellOfReadAriticle = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfReadAriticle"];
    [cellOfReadAriticle.myImageView sd_setImageWithURL:[NSURL URLWithString:_mArrayOfData[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"0"]];
    cellOfReadAriticle.titleLabel.text = _mArrayOfData[indexPath.row].title;
    cellOfReadAriticle.authorLabel.text = _mArrayOfData[indexPath.row].author;
    cellOfReadAriticle.viewnumLabel.text = _mArrayOfData[indexPath.row].viewnum;
    return cellOfReadAriticle;
}
#pragma mark - 返回每个 row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT / 6;
}
#pragma mark - tableView 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VCOfReadWeb *vcOfReadWeb = [[VCOfReadWeb alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vcOfReadWeb];
    NSLog(@"%@", self.nextResponder.nextResponder.nextResponder);
    VCOfReadArticle *vcOfReadArticle = (VCOfReadArticle *)self.nextResponder.nextResponder.nextResponder;
    vcOfReadWeb.stringURL = [NSString stringWithFormat:@"http://yiapi.xinli001.com/v2/yi/article-detail-newpage/%@?user_id=1004358517&_version=4.0", _mArrayOfData[indexPath.row].ID];
    vcOfReadWeb.myID = _mArrayOfData[indexPath.row].ID;
    [vcOfReadArticle presentViewController:navi animated:YES completion:^{
        
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
